/**
 * 版权声明：本文为博主原创文章，遵循 CC 4.0 BY-SA 版权协议，转载请附上原文出处链接和本声明。
 * 本文链接：https://blog.csdn.net/chubu3218/article/details/123920304
 *
 * modified by kevin
 */

import QtQuick 2.14
import QtQuick.Layouts 1.12
import QtQuick.Controls.Material 2.12
import QtQuick.Controls 2.14
import QtQuick.Window 2.13
import cn.JsonDataCom 1.0

Rectangle {
    id:root

    width: 800
    height: 400
    color: "lightblue"

    property int itemHeight: 35
    property int itemCounter: 0
    property var comArray: new Array
    property int count: comArray.length

    //读写Json数据
    JsonDataCom{
        id:jsonCom
    }

    Button{
        id:btnAdd
        y: 5
        text: "添加"
        anchors.right: root.right
        anchors.rightMargin: 5
        onClicked:createCom()
    }

    Button{
        id:btnSave
        text: "保存Json"
        anchors.right: root.right
        anchors.rightMargin: 5
        anchors.top: btnAdd.bottom
        anchors.topMargin: 5
        onClicked: saveJson()
    }

    Text {
        id: element
        x: 706
        y: 105
        width: 79
        height: 168
        text: qsTr("添加几条数据，然后保存，下次就会自动加载数据")
        anchors.right: parent.right
        anchors.rightMargin: 16
        wrapMode: Text.WordWrap
        font.pixelSize: 14
    }

    Component{
        id:itemCom
        Rectangle {
            id:rectCom
            color: "#F0F0F0"
            width: parent.width
            height:itemHeight
            property var myID: 0

            signal objectdestroy(var num)

            RowLayout{
                id:jsonDataItem
                height:itemHeight
                clip: true
                Layout.alignment:Qt.AlignHCenter | Qt.AlignBottom

                Label{
                    id:itemNo
                    color: "blue"
                    text: myID
                    Layout.preferredWidth: 24
                    horizontalAlignment: Label.AlignHCenter
                    Layout.alignment: Qt.AlignVCenter | Qt.AlignHCenter
                }

                Label{
                    text:"距离下限:"
                    Layout.alignment: Qt.AlignVCenter | Qt.AlignRight
                }
                TextField{
                    id:disDown
                    Layout.preferredWidth: 80
                    horizontalAlignment: Label.AlignHCenter
                    verticalAlignment: Label.AlignVCenter
                    text: myRandom()
                }
                Label{
                    text:"距离上限:"
                    Layout.alignment: Qt.AlignVCenter | Qt.AlignRight
                }
                TextField{
                    id:disUp
                    Layout.preferredWidth: 80
                    horizontalAlignment: Label.AlignHCenter
                    verticalAlignment: Label.AlignVCenter
                    text: myRandom()+1000
                }
                Label{
                    text:"速度下限:"
                    Layout.alignment: Qt.AlignVCenter | Qt.AlignRight
                }
                TextField{
                    id:spdDown
                    Layout.preferredWidth: 80
                    horizontalAlignment: Label.AlignHCenter
                    verticalAlignment: Label.AlignVCenter
                    text: myRandom()
                }
                Label{
                    text:"速度上限:"
                    Layout.alignment: Qt.AlignVCenter | Qt.AlignRight
                }
                TextField{
                    id:spdUp
                    Layout.preferredWidth: 80
                    horizontalAlignment: Label.AlignHCenter
                    verticalAlignment: Label.AlignVCenter
                    text: myRandom()+1000
                }
                Button{
                    id:delArea
                    Layout.preferredWidth: 80
                    Layout.fillHeight: true
                    text: "删除"
                    onClicked: {
                        rectCom.destroy()
                        rectCom.objectdestroy(rectCom.myID)
                    }
                }
            }
            function myRandom(){
                return Math.round(1000 *Math.random())
            }

            function getItem(){
                let data = {}
                data["disUp"] = disUp.text
                data["disDown"] = disDown.text
                data["spdUp"] = spdUp.text
                data["spdDown"] = spdDown.text

                return data
            }

            function setItem(data){
                if (!data) return;
                disUp.clear()
                disUp.insert(0,data.disUp)
                disDown.clear()
                disDown.insert(0,data.disDown)
                spdUp.clear()
                spdUp.insert(0,data.spdUp)
                spdDown.clear()
                spdDown.insert(0,data.spdDown)
            }

            Component.onCompleted: {
                itemCounter++
                myID = itemCounter
                y = comArray.length*40
            }
        }
    }

    Rectangle {
        id: mainColumn
        anchors.right: btnAdd.left
        anchors.bottom: parent.bottom
        anchors.left: parent.left
        anchors.top: parent.top
        anchors.rightMargin: 5
        color: "transparent"
        anchors.leftMargin: 5
        anchors.bottomMargin: 5
        anchors.topMargin: 5
    }


    Component.onCompleted: {
        readJson()
    }

    function createCom(data){
        var com = itemCom.createObject(mainColumn)

        com.objectdestroy.connect(deleteCom)
        com.parent = mainColumn
        com.setItem(data)
        comArray.push(com)
    }

    function deleteCom(num){
        var find= -1
        for(var i in comArray){
            if(find >= 0){
                comArray[i].x -= 0
                comArray[i].y -= 40
            }

            if(comArray[i].myID === num){
                find = i
            }
        }

        // find one
        if(find >=0){
            comArray.splice(find,1)
        }
    }

    function readJson(){
        var ec = jsonCom.readData();
        if (ec > 0) return;

        var bgVec = jsonCom.getData()
        for(let obj of bgVec){
            createCom(obj)//动态初始化组件参数
        }
    }
    function saveJson(){
        var data = new Array
        for(var obj of comArray){
            let item = obj.getItem()
            data.push(item)
        }
        jsonCom.saveData(data) //存储Json数据
    }
}


